<template>
  <div>
    <f-search-form :more='true' @searchHandler='searchHandler'>
      <f-search-form-item label='订单日期' type='date' name='beginDate' />
      <f-search-form-item label='订单编号' name='orderNo' />
      <f-search-form-item label='收货地址' name='customAddress' />
    </f-search-form>
    <f-table ref='table' rowKey='orderId'
             :tableColumns='columns'
             :showSetting='false'
             :scroll='{x:true}'
             :multiple='true'
             :operator='operator'
             :showMultipleBtn='false'
             :params='queryParam'
             :api='api.hyorder'
             handler='hyWaybillTab1ListHandler'>
      <span slot='action' slot-scope='text, record'>
          <template>
             <a href='javascript:;' @click='$refs.printDetailsDrawer.showDrawer(record)'>明细</a>
            <a-divider type='vertical' />
            <a @click='print(record)' v-if='record.outPrintTag==0'>打印面单</a>
            <a @click='printOrderDetails(record)' v-if='record.outPrintTag2==0 && record.outPrintTag>0'>打印标签</a>
             <a-divider type='vertical' />
          </template>
         <a-dropdown>
            <a class='ant-dropdown-link'>更多<a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item>
                 <a @click='printView(record)'>预览面单</a>
              </a-menu-item>
              <a-menu-item v-if='record.outPrintTag>0'>
                  <a @click='print(record)'>补打面单</a>
              </a-menu-item>
              <a-menu-item v-if='record.outPrintTag==0'>
                <a href='javascript:;' @click='setOutPrintTag(record)'>取消面单打印</a>
              </a-menu-item>
               <a-menu-item v-if='record.outPrintTag>0'>
                <a href='javascript:;' @click='setOutPrintTag2(record)'>取消标签打印</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
    </f-table>
    <FlymePrintView ref='preView' />
    <print-details-drawer ref='printDetailsDrawer'></print-details-drawer>
    <a-modal title='物流面单打印确认' v-model='printModal' width='500px' height='500px' @cancel='this.printModal=false' @ok='mergePrint'>
      总件数:
      <a-input-number id='inputNumber' style='width:200px' v-model='totalDrugJs' :min='1' />
    </a-modal>
    <a-modal title='物流标签打印确认' v-model='printModal2' width='500px' height='500px' @cancel='this.printModal2=false' @ok='mergeDetailsPrint'>
      总件数:
      <a-input-number id='inputNumber' style='width:200px' v-model='totalDrugJs' :min='1' />
    </a-modal>
    <a-modal title='标签打印确认' v-model='printModal3' width='500px' height='500px' @cancel='this.printModal3=false' @ok='printOrderDetails'>
      总件数:
      <a-input-number id='inputNumber' style='width:200px' v-model='totalDrugJs' :min='1' />
    </a-modal>
  </div>
</template>
<script>
import api from "../../js/api"
import FlymePrintView from "@/components/Flyme/FlymePrintDesigner/preview"
import PrintDetailsDrawer from "@/views/module/haiying/order/waybill/components/print_details_drawer"

export default {
  name: "Tab1",
  components: { PrintDetailsDrawer, FlymePrintView },
  data() {
    let me = this
    //操作按钮
    let operator = []
    operator.push({
      name: "合并打印物流面单",
      that: this,
      icon: "printer",
      handler: (table, that) => {
        let selectIds = table.selectedRowKeys
        let totalDrugJs = table.getSelectedTotal("drugJsCount")
        this.totalDrugJs = totalDrugJs
        if (selectIds.length > 0) {
          this.openPrintModal(selectIds)
        } else {
          that.$message.error("请选择合并打印数据")
        }
      }
    })
    /* operator.push({
       name: "合并打印标签",
       that: this,
       icon: "printer",
       handler: (table, that) => {
         let selectIds = table.selectedRowKeys
         let totalDrugJs = table.getSelectedTotal("drugJsCount")
         this.totalDrugJs = totalDrugJs
         if (selectIds.length > 0) {
           this.openPrintModal2(selectIds)
         } else {
           that.$message.error("请选择合并打印数据")
         }
       }
     })*/
    /* operator.push({
       name: "批量打印",
       that: this,
       batch: true,
       icon: "printer",
       handler: (table, that) => {
         let selectIds = table.selectedRowKeys
         if (selectIds.length > 0) {
           this.batchPrint(selectIds)
         } else {
           that.$message.error("请选择打印数据")
         }
       }
     })*/
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      printModal: false,
      printModal2: false,
      printModal3: false,
      printCount: 1,
      totalDrugJs: 1,
      orderIds: null,
      orderDetailsIds: null,
      activeKey: "0",
      // 表头
      columns: [
        {
          title: "序号",
          width: "60px",
          align: "center",
          fixed: "left",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "出库单号",
          align: "left",
          fixed: "left",
          dataIndex: "outOrderNo"
        },
        {
          title: "单据日期",
          align: "left",
          width: "100px",
          dataIndex: "sendDate"
        },
        {
          title: "单位名称",
          align: "left",
          ellipsis: true,
          width: "130px",
          dataIndex: "customName"
        },
        {
          title: "收货地址",
          align: "left",
          width: "150px",
          dataIndex: "customAddress"
        },
        {
          title: "联系人",
          align: "left",
          width: "80px",
          dataIndex: "customUserName"
        },
        {
          title: "联系电话",
          align: "left",
          width: "100px",
          dataIndex: "customUserTel"
        },
        {
          title: "部门",
          align: "left",
          dataIndex: "deptName"
        },
        {
          title: "业务员",
          align: "left",
          width: "80px",
          dataIndex: "saleManName"
        },
        {
          title: "总件数",
          align: "left",
          fixed: "right",
          dataIndex: "drugJsCount"
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          width: "170px",
          fixed: "right",
          scopedSlots: { customRender: "action" }
        }
      ],
      operator: operator,
      processImageVisible: false,
      processInstanceId: null
    }
  },
  created() {
  },
  methods: {
    onLoad() {
      this.$refs.table.refresh()
    },
    /**
     * 合并打印面单
     * @param orderIds
     */
    openPrintModal(orderIds) {
      this.orderIds = orderIds
      this.printModal = true
    },
    /**
     * 合并打印标签
     * @param orderIds
     */
    openPrintModal2(orderIds) {
      this.orderIds = orderIds
      this.printModal2 = true
    },
    /**
     * 打印标签
     * @param record
     */
    openPrintModal3(record) {
      this.orderDetailsIds = record.orderDetailsIds
      this.orderId = record.orderId
      this.totalDrugJs = record.drugJsCount
      this.printModal3 = true
    },
    //打印预览
    printView(order) {
      this.http.post(this, {
        url: api.hyorder.getOutOrderPrint,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          let template = data.template
          let printDate = data.dataList[0]
          let printTemplate = JSON.parse(template)
          this.$refs.preView.printView(printTemplate, printDate, 200)
        }
      })
    },
    //直接打印
    print(order) {
      this.http.confirm(this, {
        content: "确认打印吗?",
        url: api.hyorder.getOutOrderPrint,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          let template = data.template
          let printData = data.dataList[0]
          let printTemplate = JSON.parse(template)
          this.$refs.preView.print2({
            title: "物流面单",
            template: printTemplate,
            printData: printData,
            printSuccess: (response) => {
              this.updatePrintState(printData.orderId)
            }
          })
        }
      })
    },
    //合并打印
    mergePrint() {
      this.http.post(this, {
        url: api.hyorder.getMergeOutOrderPrint,
        data: {
          orderId: this.orderIds.join(",")
        },
        success: (me, data) => {
          this.printModal = false
          let template = data.template
          let printData = data.dataList[0]
          let printTemplate = JSON.parse(template)
          printData["totalDrugJs"] = this.totalDrugJs
          this.$refs.preView.print2({
            title: "物流面单",
            template: printTemplate,
            printData: printData,
            printSuccess: (response) => {
              this.updatePrintState(this.orderIds.join(","))
            }
          })
        }
      })
    },
    //合并打印
    mergeDetailsPrint() {
      this.http.post(this, {
        url: api.hyOrderDetails.getMergeOutOrderPrint,
        data: {
          orderId: this.orderIds.join(",")
        },
        success: (me, data) => {
          this.printModal2 = false
          let totalDrugJs = this.totalDrugJs
          let template = data.template
          let printData = data.dataList
          let printTemplate = JSON.parse(template)
          this.$refs.preView.batchPrintByCount({
            template: printTemplate,
            dataList: printData,
            countKey: "drugJsCount",
            countVal: totalDrugJs,
            printSuccess: (response) => {
              this.updatePrintState2(this.orderIds.join(","))
            }
          })
        }
      })
    },
    //批量打印
    batchPrint(orderIds) {
      this.http.confirm(this, {
        content: "确认打印吗?",
        url: api.hyorder.getOutOrderPrint,
        data: {
          orderId: orderIds.join(",")
        },
        success: (me, data) => {
          let template = data.template
          let printDate = data.dataList
          let printTemplate = JSON.parse(template)
          this.$refs.preView.batchPrint(printTemplate, printDate, (response) => {
            this.updatePrintState(response.orderId)
          })
        }
      })
    },
    //打印标签
    printOrderDetails(record) {
      this.http.confirm(this, {
        title:"标签打印确认",
        content: "提示:明细中可修改打印数量",
        url: api.hyOrderDetails.getOutOrderPrint,
        data: {
          orderDetailsId: record.orderDetailsIds,
          customCount: record.drugJsCount
        },
        success: (me, data) => {
          this.printModal3 = false
          let totalDrugJs = data.drugJsCount
          let template = data.template
          let printDate = data.dataList
          let printTemplate = JSON.parse(template)
          this.$refs.preView.batchPrintByCount({
            template: printTemplate,
            dataList: printDate,
            countKey: "drugJsCount",
            countVal: totalDrugJs,
            printSuccess: (response) => {
              this.updatePrintState2(record.orderId)
            }
          })
        }
      })
    },
    /**
     * 取消出库单打印
     * @param orderIds
     */
    setOutPrintTag(record) {
      this.http.confirm(this, {
        content: "确认取消打印吗?",
        url: api.hyorder.setOutPrintTag,
        data: {
          orderId: record.orderId,
          outPrintTag: -1
        },
        success: (me, data) => {
          this.$message.success("操作成功")
          this.onLoad()
        }
      })
    },
    /**
     * 取消标签打印
     * @param orderIds
     */
    setOutPrintTag2(record) {
      this.http.confirm(this, {
        content: "确认取消打印吗?",
        url: api.hyorder.setOutPrintTag2,
        data: {
          orderId: record.orderId,
          outPrintTag2: -1
        },
        success: (me, data) => {
          this.$message.success("操作成功")
          this.onLoad()
        }
      })
    },
    updatePrintState(orderId) {
      this.http.post(this, {
        url: api.hyorder.updateOutPrintTag,
        data: {
          orderId: orderId
        },
        success: (me, data) => {
          this.$message.success("打印成功")
          this.onLoad()
        }
      })
    },
    updatePrintState2(orderId) {
      this.http.post(this, {
        url: api.hyorder.updateOutPrintTag2,
        data: {
          orderId: orderId
        },
        success: (me, data) => {
          this.$message.success("打印成功")
          this.onLoad()
        }
      })
    },
    changeHandler(activeKey) {
      this.activeKey = activeKey
      this.$refs.table.refresh(true, { activeKey: this.activeKey })
    }
  }
}
</script>
